package cn.demo.controller;

import cn.demo.Util.Page;
import cn.demo.Util.Result;
import cn.demo.entity.Bill;
import cn.demo.entity.provider;
import cn.demo.service.BillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author weizhongli
 * @Date 2020/3/22
 * @Version v1.0
 * @Description 订单管理控制
 */

@Controller
public class BillController {

    @Autowired
    private BillService billService;

    /*
     * 查询所有订单
     * */
    @ResponseBody
    @RequestMapping("/proList")
    public Result proList() {
        try {
            List<provider> providers = billService.proList();
            return new Result(true, "查询成功", providers);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }

    @RequestMapping("/showBillList")
    public String showBillList() {
        return "billList";
    }


    /*
     * 模糊查询
     * */
    @ResponseBody
    @RequestMapping("/selectbill")
    public Result sbill(String productName, @RequestParam(defaultValue = "0") int providerId, @RequestParam(defaultValue = "0") int isPayment, @RequestParam(defaultValue = "1") int PageIndex, @RequestParam(defaultValue = "2") int PageSize) {
        try {
            Page page = billService.selectBillList(PageIndex, PageSize, productName, providerId, isPayment);
            return new Result(true, "查询成功", page);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }


    /*
     * 添加订单
     * */
    @ResponseBody
    @RequestMapping("/insertbill")
    public Result insertbill(Bill bill) {
        try {
            int bill1 = billService.insertBill(bill);
            return new Result(true, "添加成功", bill1);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加失败");
        }
    }

    /*
    * 修改订单
    * */
    @RequestMapping("billUpdate")
    public String billUpdate(int id, HttpServletRequest request) {
        List<Bill> bills = billService.selectBillandId(id);
        request.setAttribute("bills", bills);
        return "billUpdate";
    }


    /*
     * 更新订单
     * */
    @ResponseBody
    @RequestMapping("/updatebill")
    public Result updatebill(Bill bill) {
        try {
            int i = billService.updateBill(bill);
            return new Result(true, "更新成功", i);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "更新失败");
        }
    }


    /*
    * 删除订单
    * */
    @ResponseBody
    @RequestMapping("/deletebill")
    public void deletebill(String str, HttpServletResponse response) throws Exception {
        PrintWriter out = response.getWriter();
        //stem.out.println(str+"===================deleteuser");
        String[] ids = str.split(",");
        int deleteuser = 0;
        for (int i = 0; i < ids.length; i++) {
            deleteuser = billService.deletebill(Integer.parseInt(ids[i].trim()));
        }
        response.getWriter().write("true");
    }


    /*
     * 导出数据
     * */
    @RequestMapping("/daochu")
    @ResponseBody
    public Result download(HttpServletRequest request) throws Exception {
        List<Bill> list = billService.selectbill();
        for (Bill item : list) {
            Date birthday = item.getCreationDate();
            SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd");
            String format = myFormat.format(birthday);
            item.setCtratedate(format);
        }
        request.getSession().setAttribute("list", list);
        return new Result(true, "导出成功", "billex");
    }


    /*
     * 数据展示
     * */
    @ResponseBody
    @RequestMapping("/billecharts")

    public Result selectbill() {
        try {
            List<Bill> selectbill = billService.selectbill();
            return new Result(true, "展示成功", selectbill);
        } catch (Exception e) {
            return new Result(false, "展示失败");
        }
    }
}